<?php  // File: _db.inc

function db_connect($user, $pwd)
{
  // use constants defined in anyco_cn.inc
  $conn = oci_connect($user, $pwd, DB_NAME, DB_CHARSET);
  if (!$conn) {
    db_error(null, __FILE__, __LINE__);
  }
  return($conn);
}

function db_do_query($conn, $statement)
{
  $stid = oci_parse($conn, $statement);
  if (!$stid) {
    db_error($conn, __FILE__, __LINE__);
  }

  $r = oci_execute($stid, OCI_DEFAULT);
  if (!$r) {
    db_error($conn, __FILE__, __LINE__);
  }
  $r = oci_fetch_all($stid, $results, null, null,
                     OCI_FETCHSTATEMENT_BY_ROW);
  return($results);
}
/*
 * execute non_select sql command
 */
function db_execute($conn, $statement)
{
  $stid = oci_parse($conn, $statement);
  if (!$stid) {
    db_error($conn, __FILE__, __LINE__);
  }

  $r = oci_execute($stid);
  if (!$r) {
    db_error($conn, __FILE__, __LINE__);
  }
  return($r);
}

// $r is the resource containing the error.
// Pass no argument or false for connection errors

function db_error($r = false, $file, $line)
{
  $err =  $r ? oci_error($r) : oci_error();

  if (isset($err['message'])) {
    $m = htmlentities($err['message']);
  }
  else {  
    $m = 'Unknown DB error';
  }

  echo '<p><b>Error</b>: at line '.$line.' of '.$file.'</p>';
  echo '<pre>'.$m.'</pre>';

  exit;
}

?>